package org.lanqiao.webfm.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javacommon.base.BaseServletAdapter;
import javacommon.web.util.ServletRequestUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.lanqiao.webfm.model.Goods;
import org.lanqiao.webfm.service.GoodsService;

/**
 * Servlet implementation class ShoppingServlet
 */
@WebServlet("/Shopping/*")
public class ShoppingServlet extends BaseServletAdapter {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		super.doPost(request, response);
		String uri = request.getRequestURI();
		if (uri.endsWith("buy.do")) {
			doBuyInternal(request, response);
		}
	}

	private void doBuyInternal(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// 获得参数
		int id = ServletRequestUtils.getIntParameter(request, "id", 0);
		// 调用业务
		HttpSession session = request.getSession();
		// 获得当前用户的购物车
		Map<Integer, Integer> cart = (Map<Integer, Integer>) session
				.getAttribute("cart");
		if (cart == null) {
			cart = new HashMap<Integer, Integer>();
		}
		// 如果同一个商品已经存在
		Integer count = cart.get(id);
		if (count != null) {
			cart.put(id, count + 1);// 重新设定该商品的数量加1
		} else {// 如果不存在,新增一个商品及其数量设定为1
			cart.put(id, 1);
		}
		// 将购物车保存在session中
		session.setAttribute("cart", cart);
		// (共享数据),转发
		response.getWriter().write("{\"result\":true}");
	}

	/**
	 * 呈现购物车
	 */
	@Override
	protected void doListInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		// 准备商品的信息<<购物车+数据库
		HttpSession session = request.getSession();
		Map<Integer, Integer> cart = (Map<Integer, Integer>) session
				.getAttribute("cart");
		// 存放完整信息的map,Goods对象--购买的数量
		Map<Goods, Integer> cartInfo = new HashMap<Goods, Integer>();
		int total=0;
		int total_dis=0;
		// 迭代购物车
		for (Iterator<Integer> iterator = cart.keySet().iterator(); iterator
				.hasNext();) {
			int goodsId = iterator.next();// 商品id
			Goods goods = goodsService.getById(goodsId);// 查询出商品信息
			Integer count = cart.get(goodsId);
			cartInfo.put(goods, count);
			total+=goods.getPrice()*count;
			total_dis+=goods.getPrice()*goods.getDiscount()*count;
		}
		request.setAttribute("cartInfo", cartInfo);
		request.setAttribute("total", total);
		request.setAttribute("total_dis", total_dis);
		request.getRequestDispatcher("/pagesf/Shopping/cart.jsp").forward(
				request, response);
	}

	/**
	 * 从购物车中删除一笔数据
	 */
	@Override
	protected void doDeleteInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		// 获得参数
		int id = ServletRequestUtils.getIntParameter(request, "id", 0);
		HttpSession session = request.getSession();
		// 获得当前用户的购物车
		Map<Integer, Integer> cart = (Map<Integer, Integer>) session
				.getAttribute("cart");
		if (cart!=null) {
			cart.remove(id);
		}
		response.getWriter().write("{\"result\":true}");
	}

	private GoodsService goodsService = GoodsService.getInstance();
}
